//
//  HYSettingCell.m
//  XPlayer
//
//  Created by 张水 on 2023/9/11.
//

#import "HYSettingCell.h"
#import "HYSettingModel.h"
#import <mach/mach.h>

@interface HYSettingCell()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *textTitleLabel;

@property (nonatomic, strong) UIImageView *arrowImageView;

@property (nonatomic, strong) UISwitch *selectSwitch;

@property (nonatomic, strong) HYSettingModel *model;

@property (nonatomic, strong) UIView *lineView;

@end

@implementation HYSettingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI 
{
    self.contentView.backgroundColor = AppContentBgThemeColor;
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.textTitleLabel];
    [self.contentView addSubview:self.arrowImageView];
    [self.contentView addSubview:self.selectSwitch];
    [self.contentView addSubview:self.lineView];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kLeftMargin_15);
        make.right.equalTo(self.contentView).offset(-4*kLeftMargin_15);
        make.top.equalTo(self.contentView).offset(kLeftMargin_15);
        make.bottom.equalTo(self.contentView).offset(-kLeftMargin_15);
    }];
    
    [self.selectSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-12);
        make.centerY.equalTo(self.contentView);
    }];
    
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-12);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@18);
        make.height.equalTo(@18);
    }];
    
    [self.textTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-12);
        make.centerY.equalTo(self.contentView);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(12);
        make.trailing.equalTo(self.contentView).offset(-12);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        make.height.mas_equalTo(kOneScale);
    }];
}

- (void)switchClick:(UISwitch *)sender 
{
    BOOL able = sender.on;
    if (self.model.dataID == kAllowNetwork){
        // 移动网络下载
        [HYMineSetManager shareManager].isAllowNetwork = able;
    }else if (self.model.dataID == kAutoPlayNext){
        // 自动播放下一个
        [HYMineSetManager shareManager].isAutoPlayNext = able;
    }else if (self.model.dataID == kAllowBackground){
        // 后台播放
        [HYMineSetManager shareManager].isAllowBackground = able;
    }else if (self.model.dataID == kAllowPrivateBrowse){
        // 无痕预览
        [HYMineSetManager shareManager].isAllowPrivateBrowse = able;
    }else if (self.model.dataID == kAllowScreenShotSavePhoto){
        // 截图后是否保存到相册
        [HYMineSetManager shareManager].isAllowScreenShotSavePhoto = able;
    }else if (self.model.dataID == kMutePlayback){
        // 静音播放
        [HYMineSetManager shareManager].isMutePlayback = able;
    }
}

-(void)setData:(HYSettingModel *)model
{
    self.model = model;
    self.titleLabel.text = model.title;
    self.lineView.hidden = model.hideLine;
    if(model.cellStyle == HYSettingCellTextOnly){
        self.selectSwitch.hidden = YES;
        self.textTitleLabel.hidden = NO;
        self.arrowImageView.hidden = YES;
        [self.textTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.contentView).offset(-12);
        }];
    }else if (model.cellStyle == HYSettingCellArrowOnly){
        self.selectSwitch.hidden = YES;
        self.textTitleLabel.hidden = YES;
        self.arrowImageView.hidden = NO;
    }else if (model.cellStyle == HYSettingCellTextArrow){
        self.selectSwitch.hidden = YES;
        self.textTitleLabel.hidden = NO;
        self.arrowImageView.hidden = NO;
        [self.textTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.contentView).offset(-30);
        }];
    }else if(model.cellStyle == HYSettingCellOption){
        self.selectSwitch.hidden = NO;
        self.textTitleLabel.hidden = YES;
        self.arrowImageView.hidden = YES;
    }
    if(model.dataID == kChangeTheme){
        NSInteger appThemeModel = [HYMineSetManager shareManager].appThemeMode;
        NSString *appThemeStr = @"自动跟随";
        if(appThemeModel == 1){
            appThemeStr = @"浅色主题";
        }else if(appThemeModel == 2){
            appThemeStr = @"深色主题";
        }
        self.textTitleLabel.text = NSLocalizedString(appThemeStr, nil);
    }else if (model.dataID == kAllowNetwork){
        //允许移动网络下载
        [self.selectSwitch setOn:[HYMineSetManager shareManager].isAllowNetwork  animated:YES];
    }else if (model.dataID == kAutoPlayNext){
        // 自动播放下一个
        [self.selectSwitch setOn:[HYMineSetManager shareManager].isAutoPlayNext animated:YES];
    }else if (model.dataID == kAllowBackground){
        // 后台播放
        [self.selectSwitch setOn:[HYMineSetManager shareManager].isAllowBackground animated:YES];
    }else if (model.dataID == kAllowPrivateBrowse){
        // 无痕浏览
        [self.selectSwitch setOn:[HYMineSetManager shareManager].isAllowPrivateBrowse animated:YES];
    }else if (model.dataID == kAllowScreenShotSavePhoto){
        // 截屏后是否允许保存至相册
        [self.selectSwitch setOn:[HYMineSetManager shareManager].isAllowScreenShotSavePhoto animated:YES];
    }else if (model.dataID == kMutePlayback){
        // 静音播放
        [self.selectSwitch setOn:[HYMineSetManager shareManager].isMutePlayback animated:YES];
    }else if (model.dataID == kMemory){
        //获取内存
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()];
        NSDictionary *deviceFreeMemoryResults = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:nil];
        // 转成double类型
        NSDictionary *totalFreeMemoryResults = [fileURL resourceValuesForKeys:@[NSURLVolumeTotalCapacityKey] error:nil];
        float totalFreeMemory = [totalFreeMemoryResults[NSURLVolumeTotalCapacityKey] floatValue] /1024/1024/1024;
        float deviceFreeMemory = [deviceFreeMemoryResults[NSURLVolumeAvailableCapacityForImportantUsageKey] floatValue] /1024/1024/1024;
        self.textTitleLabel.text = [NSString stringWithFormat:@"%.2fG/%.2fG",deviceFreeMemory,totalFreeMemory];
    }
}

- (UILabel *)titleLabel {
    if(_titleLabel == nil){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = APPTextThemeColor;
        _titleLabel.font = FONT_SIZE(16);
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UILabel *)textTitleLabel {
    if(_textTitleLabel == nil){
        _textTitleLabel = [[UILabel alloc]init];
        _textTitleLabel.textColor = APPTextThemeColor;
        _textTitleLabel.font = FONT_SIZE(14);
    }
    return _textTitleLabel;
}

- (UIImageView *)arrowImageView {
    if(_arrowImageView == nil){
        _arrowImageView = [[UIImageView alloc]init];
        _arrowImageView.image = [UIImage imageNamed:@"icon_arrow"];
    }
    return _arrowImageView;
}

- (UISwitch *)selectSwitch {
    if(_selectSwitch == nil){
        _selectSwitch = [[UISwitch alloc]init];
        _selectSwitch.layer.cornerRadius = 15.5f;
        _selectSwitch.layer.masksToBounds = YES;
        [_selectSwitch addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
    }
    return _selectSwitch;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#D1D1D1" darkModeHex:@"#232225"];
    }
    return _lineView;
}

@end
